package org.btik.light.mobile.ui.home.handle;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import org.btik.context.SimpleContext;
import org.btik.light.meta.DevArea;
import org.btik.light.mobile.R;
import org.btik.light.mobile.common.handler.nbi.NbiAgent;
import org.btik.light.mobile.meta.mo.DefaultDevMo;
import org.btik.light.mobile.common.handler.CmdSender;
import org.btik.light.mobile.ui.factory.DevPanel;
import org.btik.light.mobile.ui.factory.DevPanelFactory;
import org.btik.light.mobile.ui.home.HomeFragment;
import org.btik.light.mobile.ui.home.HomeViewModel;
import org.btik.light.tool.type.StringUtil;

import static org.btik.light.mobile.platform.conf.AppConfig.LIGHT_SERVER_HOST;
import static org.btik.light.mobile.platform.conf.ContextGlobalNames.CURRENT_DEV;

public class OpenDevPanel implements AdapterView.OnItemClickListener {

    private final HomeViewModel homeViewModel;

    private final HomeFragment homeFragment;

    public OpenDevPanel(HomeViewModel homeViewModel, HomeFragment homeFragment) {
        this.homeViewModel = homeViewModel;
        this.homeFragment = homeFragment;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        DefaultDevMo item = homeViewModel.getAdapter().getItem(position);
        if (item == null) {
            return;
        }
        SimpleContext.set(CURRENT_DEV, item);
        CmdSender.onDevChecked(item);
        if (item.getDevArea() == DevArea.CLOUD) {
            String devType = item.getDevType();
            // 其它设备没有调通，先写死
            if(!"camCar".equals(devType)){
                Toast.makeText(view.getContext(), R.string.current_server_dev_not_support, Toast.LENGTH_SHORT).show();
                return;
            }
            String lightServerHost = SimpleContext.get(LIGHT_SERVER_HOST);
            if (StringUtil.isEmpty(lightServerHost)) {
                Toast.makeText(view.getContext(), R.string.server_host_cleared, Toast.LENGTH_SHORT).show();
                return;
            }
            NbiAgent.setAddress(lightServerHost, view.getContext());
        }

        openDevPanel(item);
    }

    private void openDevPanel(DefaultDevMo item) {
        switch (item.getDevPanelType()) {
            case Activity:
                startActivity(item);
                break;
            case Fragment:
                openFragment(item);
                break;
            default:
                System.out.println("type is" + item.getDevPanelType());
        }

    }

    private void startActivity(DefaultDevMo item) {
        FragmentActivity activity = homeFragment.getActivity();
        if (activity == null) {
            return;
        }
        String name = DevPanelFactory.getDevPanelType(item.getDevType()).getName();
        Intent intent = new Intent(name);
        SimpleContext.set(item.getSn(), item);
        activity.startActivity(intent);
    }

    private void openFragment(DefaultDevMo item) {

        DevPanel<?> devPanel = DevPanelFactory.newDevPanel(item);
        if (devPanel == null) {
            return;
        }
        homeFragment.hideNav();
        FragmentManager fragmentManager = homeViewModel.getFragmentManager();
        FragmentTransaction fTransaction = fragmentManager.beginTransaction();
        fTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        fTransaction.replace(R.id.nav_host_fragment_activity_main, devPanel);
        fTransaction.addToBackStack(null);
        fTransaction.commit();
    }
}
